package kitrobot.com.bed.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import kitrobot.com.bed.CountDownUtil;
import kitrobot.com.bed.Service.IdentifyingService;
import kitrobot.com.bed.Service.ModifyService;
import kitrobot.com.wechat_bottom_navigation.R;
public class ForgetActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText et_ftel;
    private TextView tv_fyan;
    private EditText et_fpsw;
    private EditText et_frp;
    private EditText et_fyan;
    private TextView tv_jg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.forget);
        Intent intent=getIntent();
        final String phone=intent.getStringExtra("phone");
        et_ftel= (EditText) findViewById(R.id.et_ftel);
        tv_fyan= (TextView) findViewById(R.id.tv_fyan);
        et_ftel.setText(phone);
        et_fpsw= (EditText) findViewById(R.id.et_fpsw);
        et_frp= (EditText) findViewById(R.id.et_frp);
        et_fyan= (EditText) findViewById(R.id.et_fyan);
        tv_fyan= (TextView) findViewById(R.id.tv_fyan);


    }
    public void Jishi(){
        new CountDownUtil(tv_fyan)

                .setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            final String phone=et_ftel.getText().toString().trim();
                                            String a= String.valueOf(isMobileNO( phone));
                                            if (phone.length()==0){
                                                Toast.makeText(ForgetActivity.this,"手机号码不能为空！",Toast.LENGTH_LONG).show();
                                            }
                                            else if(a.equals("false")||phone.length()<11){
                                                Toast.makeText(ForgetActivity.this,"请输入正确的手机号码！",Toast.LENGTH_LONG).show();
                                            }
                                            else {
                                                new Thread(){
                                                    public void run(){
                                                        final String result= IdentifyingService.IdentifyingByClientPost(phone);
                                                        if (result!=null){
                                                            runOnUiThread(new Runnable() {
                                                                @Override
                                                                public void run() {
                                                                    Toast.makeText(ForgetActivity.this,result,Toast.LENGTH_LONG).show();
                                                                }
                                                            });
                                                        }
                                                        else{
                                                            runOnUiThread(new Runnable() {
                                                                @Override
                                                                public void run() {
                                                                    Toast.makeText(ForgetActivity.this,"请求失败",Toast.LENGTH_LONG).show();
                                                                }
                                                            });
                                                        }
                                                    };

                                                }.start();
                                                Jishi();
                                            }
                                        }
                                    }
                )
                .setCountDownMillis(60_000L)//倒计时60000ms
                .setCountDownColor(android.R.color.holo_blue_light,android.R.color.darker_gray)//不同状态字体颜色
                .start();
    }
    @Override
    public void onClick(View v) {

    }
    //获取信息进行修改密码
    public void Modify(View view) {
        final String phone=et_ftel.getText().toString().trim();
        final String password=et_fpsw.getText().toString().trim();
        final String password2=et_frp.getText().toString().trim();
        final String code=et_fyan.getText().toString().trim();
        String a= String.valueOf(isMobileNO( phone));
        if (phone.length()==0){
            Toast.makeText(ForgetActivity.this,"手机号码不能为空！",Toast.LENGTH_LONG).show();
        }
        else if(a.equals("false")||phone.length()<11){
            Toast.makeText(ForgetActivity.this,"请输入正确的手机号码！",Toast.LENGTH_LONG).show();
        }
        else if(password.length()==0){
            Toast.makeText(ForgetActivity.this,"密码不能为空！",Toast.LENGTH_LONG).show();
        }
        else if(password.length()<6){
            Toast.makeText(ForgetActivity.this,"密码不能小于6位！",Toast.LENGTH_LONG).show();
        }
        else if(password2.length()==0){
            Toast.makeText(ForgetActivity.this,"确认密码不能为空！",Toast.LENGTH_LONG).show();
        }
        else if(!password.equals(password2)){
            Toast.makeText(ForgetActivity.this,"两次输入密码不一致！",Toast.LENGTH_LONG).show();
        }
        else if(code.length()==0){
            Toast.makeText(ForgetActivity.this,"验证码不能为空！",Toast.LENGTH_LONG).show();
        }
        else {
            new Thread(){
                public void run(){
                    final String result= ModifyService.ModifyByClientPost(phone,password,code);
                    if (result!=null){
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(ForgetActivity.this,result,Toast.LENGTH_LONG).show();
                            }
                        });
                    }
                    else{
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(ForgetActivity.this,"请求失败",Toast.LENGTH_LONG).show();
                            }
                        });
                    }
                };

            }.start();
        }
    }

    public void Yanzheng(View view) {
        final String phone=et_ftel.getText().toString().trim();
        String a= String.valueOf(isMobileNO( phone));
        if (phone.length()==0){
            Toast.makeText(ForgetActivity.this,"手机号码不能为空！",Toast.LENGTH_LONG).show();
        }
        else if(a.equals("false")||phone.length()<11){
            Toast.makeText(ForgetActivity.this,"请输入正确的手机号码！",Toast.LENGTH_LONG).show();
        }
        else {
            new Thread(){
                public void run(){
                    final String result= IdentifyingService.IdentifyingByClientPost(phone);
                    if (result!=null){
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(ForgetActivity.this,result,Toast.LENGTH_LONG).show();
                            }
                        });
                    }
                    else{
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(ForgetActivity.this,"请求失败",Toast.LENGTH_LONG).show();
                            }
                        });
                    }
                };

            }.start();
            Jishi();
        }

    }
    //使用正则表达式判断
    public static boolean isMobileNO(String mobiles) {
        //"[1]"代表第1位为数字1，"[358]"代表第二位可以为3、5、8中的一个，"\\d{9}"代表后面是可以是0～9的数字，有9位。
        String telRegex = "13\\d{9}|14[57]\\d{8}|15[012356789]\\d{8}|18[01256789]\\d{8}|17[0678]\\d{8}"; ;
        if (TextUtils.isEmpty(mobiles)) return false ;
        else return mobiles.matches( telRegex ) ;
    }
    public void Returnto(View view){
        Intent intent=new Intent();
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setClass(this,LoginActivity.class);
        startActivity(intent);

    }


}
